package site.wanjiahao.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigPropertiesHolder {

    static Properties properties;

    static {
        try (InputStream resourceAsStream = ConfigPropertiesHolder.class.getClassLoader().getResourceAsStream("config.properties")) {
            properties = new Properties();
            properties.load(resourceAsStream);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    public static Serialize.SerializeAlgorithm getSerializeAlgorithm() {
        Object res = properties.get("serialize");
        if (res == null || "".equals(res)) {
            return Serialize.SerializeAlgorithm.JSON;
        } else {
            return Serialize.SerializeAlgorithm.valueOf(res.toString());
        }
    }

    public static Class<?> getImpl(Class<?> clazz) throws ClassNotFoundException {
        // 根据接口类型获取对应的实现类
        String impl = (String) properties.get(clazz.getName());
        return Class.forName(impl);
    }

}
